import math
from OpenGL.GL import *

class Vec3:
    def __init__(self, x = 0, y = 0, z = 0):
        self.x = x
        self.y = y
        self.z = z
        
    def __add__(self, v):
        return Vec3(self.x + v.x, self.y + v.y, self.z + v.z)
    
    def __sub__(self, v):
        return Vec3(self.x - v.x, self.y - v.y, self.z - v.z)
    
    def __mul__(self, v):
        """pairwise multiply - not dot product"""
        if v.__class__ == Vec3:
            return Vec3(self.x * v.x, self.y * v.y, self.z * v.z)
        else:
            return Vec3(self.x * v, self.y * v, self.z * v)

    def __rmul__(self, v):
        if v.__class__ == Vec3:
            return Vec3(self.x * v.x, self.y * v.y, self.z * v.z)
        else:
            return Vec3(self.x * v, self.y * v, self.z * v)

    def __div__(self, v):
        if v.__class__ == Vec3:
            return Vec3(self.x / v.x, self.y / v.y, self.z / v.z)
        else:
            return Vec3(self.x / v, self.y / v, self.z / v)

    def __repr__(self):
        return "<Vec3 %s %s %s>" % (str(self.x),str(self.y),str(self.z))
        
    def dot(self, v):
        return self.x * v.x + self.y * v.y + self.z * v.z
    
    def cross(self, v):
        return Vec3(self.y * v.z - self.z * v.y, self.z * v.x - self.x * v.z, self.x * v.y - self.y * v.x)
    
    def length(self):
        return math.sqrt(self.x * self.x + self.y * self.y + self.z * self.z)
    
    def norm(self):
        d = self.length()
        if d == 0:
            return Vec3(0,0,0)
        else:
            return self / d
        
    def vertex(self):
        glVertex3f(self.x,self.y,self.z)
        
    def translate(self):
        glTranslatef(self.x,self.y,self.z)
        
    def scale(self):
        glScalef(self.x,self.y,self.z)
        
    def color(self):
        glColor3f(self.x,self.y,self.z)
